//
//  ViewPictureViewController.m
//  ZooNgonDoiOS
//
//  Created by Trung sound on 8/28/12.
//  Copyright (c) 2012 Personal. All rights reserved.
//

#import "ViewPictureViewController.h"
#import "UIImage+Crop.h"


@implementation ViewPictureViewController
@synthesize viewPictureDelegate;

#define IMAGE_VIEW_SIZE 216

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        [self initHeaderView];
    }
    return self;
}

- (id)initWithObject:(NSArray *)arPic {
    self = [super initWithNibName:nil bundle:nil];
    if (self) {
        _arViewPicture = [[NSMutableArray alloc]initWithArray:arPic];
        [self initHeaderView];
        [self initContentView];

    }
    
    return self;
}

- (void)initHeaderView {
    _vHeader = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 48)];
    _vHeader.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:IMG_NAV_BACKGROUND]];
    
    _btnBack = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 48, 48)];
    [_btnBack setBackgroundImage:[UIImage imageNamed:IMG_BACK_NORMAL] forState:UIControlStateNormal];
    [_btnBack setBackgroundImage:[UIImage imageNamed:IMG_BACK_SELECTED] forState:UIControlStateHighlighted];
    [_btnBack addTarget:self action:@selector(backClicked) forControlEvents:UIControlEventTouchUpInside];
    
    _lbTitle = [[UILabel alloc]initWithFrame:CGRectMake((self.view.bounds.size.width -150)/2, 10, 150, 25)];
    [_lbTitle setText:@"Tùy chỉnh ảnh"];
    [_lbTitle setFont:[UIFont systemFontOfSize:18]];
    [_lbTitle setBackgroundColor:[UIColor clearColor]];
    [_lbTitle setTextColor:[UIColor whiteColor]];
    
    _btnCheckIn = [[UIButton alloc] initWithFrame:CGRectMake(self.view.bounds.size.width * 3/4, 0, self.view.bounds.size.width/4, 48)];
        //    [_btnCheckIn setBackgroundImage:[UIImage imageNamed:IMG_CHECKIN_NORMAL] forState:UIControlStateNormal];
        //    [_btnCheckIn setBackgroundImage:[UIImage imageNamed:IMG_CHECKIN_SELECTED] forState:UIControlStateHighlighted];
    
    [_btnCheckIn addTarget:self action:@selector(pushPickFoodScreen) forControlEvents:UIControlEventTouchUpInside];

    [_vHeader addSubview:_btnBack];
    [_vHeader addSubview:_lbTitle];
    [_vHeader addSubview:_btnCheckIn];
    
    [self.view addSubview:_vHeader];
    
    [_btnBack release];
    [_btnCheckIn release];
    [_vHeader release];

}

- (void)initContentView {
    _scPicture = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 48, 320, 280)];
    [_scPicture setBackgroundColor:[UIColor whiteColor]];
    [_scPicture setPagingEnabled:YES];	
    CGRect workingFrame = _scPicture.frame;
	workingFrame.origin.x = 0;
    workingFrame.origin.y = 20;
    int i = 0;

	for(NSDictionary *dict in _arViewPicture) {
        
        _vMain = [[UIView alloc]initWithFrame:CGRectMake(0, 5, 245, 245)];
        [_vMain setBackgroundColor:[UIColor blueColor]];
        UIView *vBgPic = [[UIView alloc]initWithFrame:CGRectMake(0, 19, 226, 226)];
        [vBgPic setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:IMG_VIEWPIC_BACKGROUND]]];
        
        UIImage *imageOriginal = [dict objectForKey:UIImagePickerControllerOriginalImage];
        NSLog(@"width: %f, height: %f",imageOriginal.size.width,imageOriginal.size.height);

        UIImage *imageCrop;

        imageCrop = [imageOriginal imageByScalingAndCroppingForSize:CGSizeMake(IMAGE_VIEW_SIZE, IMAGE_VIEW_SIZE)];
        
		UIImageView *imageview = [[UIImageView alloc] initWithImage:imageCrop];
        imageview.frame = CGRectMake(4, 4, IMAGE_VIEW_SIZE, IMAGE_VIEW_SIZE);        
        
//        [imageview setContentMode:UIViewContentModeScaleAspectFit];
        
        NSURL *url;
        url = [dict objectForKey:UIImagePickerControllerReferenceURL];
        NSLog(@"%@",url);
        
        [vBgPic addSubview:imageview];
        [_vMain addSubview:vBgPic];
        
        UIButton *btnDelPic = [[UIButton alloc]initWithFrame:CGRectMake(207, 0, 38, 38)];
        [btnDelPic addTarget:self action:@selector(btnDelPicClicked:) forControlEvents:UIControlEventTouchUpInside];
        [btnDelPic setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:IMG_BTN_VIEWPIC_DEL]]];
        _vMain.tag = i;
        btnDelPic.tag = i++;
        
        [_vMain addSubview:btnDelPic];
        
            //		[_vMain setContentMode:UIViewContentModeScaleAspectFit];
		_vMain.frame = workingFrame;
        
		
        
		[_scPicture addSubview:_vMain];
		[imageview release];
        [vBgPic release];
        [_vMain release];
		
		workingFrame.origin.x = workingFrame.origin.x + workingFrame.size.width;
	}
    
    
	[_scPicture setContentSize:CGSizeMake(workingFrame.origin.x, workingFrame.size.height)];
    [self.view addSubview:_scPicture];
    [_scPicture release];
    
}

- (void)btnDelPicClicked:(UIButton *)button {
   
    [_arViewPicture removeObjectAtIndex:button.tag];
    [self initContentView];
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

- (void)backClicked {
    
    if (viewPictureDelegate != nil) {
        [viewPictureDelegate viewPictureControllerDismissed:_arViewPicture];
    }
    
    [self dismissModalViewControllerAnimated:YES];
}

#pragma mark - View lifecycle

/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView
{
}
*/

/*
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{
    [super viewDidLoad];
}
*/

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

@end
